package com.geuso.xingdutch;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.geuso.xingdutch.util.ClickUtil;

public class MainMenuListAdapter extends ArrayAdapter<MainMenuListItem> {
	private static final int resource = R.layout.main_list_item;
	
	public MainMenuListAdapter(Context context, ArrayList<MainMenuListItem> items) {
		super(context, resource, items);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View v = convertView;
		
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(resource, null);
		}

		final MainMenuListItem i = getItem(position);
		if (i != null) {
			TextView name = (TextView) v.findViewById(R.id.main_list_item_text);
			
			if (name != null) name.setText(getContext().getString(i.getNameResource()));
			
			if (this.getContext() instanceof Activity){
				v.setOnClickListener(ClickUtil.startActivityOnClick(i.getActivity(), (Activity) this.getContext()));
			}		
		}
		
		v.setBackgroundResource(R.drawable.list_selector);
		
		return v;
	}
}
